package com.hwadee.ems.service.impl;

import jakarta.annotation.PostConstruct;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hwadee.ems.domain.po.Classroom;
import com.hwadee.ems.domain.dto.PageQuery;
import com.hwadee.ems.domain.dto.Result;
import com.hwadee.ems.domain.vo.ClassroomVO;
import com.hwadee.ems.mapper.ClassroomMapper;
import com.hwadee.ems.service.IClassroomService;

import cn.hutool.core.util.StrUtil;

/**
 * <p>
 * 教室 服务实现类
 * </p>
 *
 * @author EnosElinsa
 * @since 2024/06/21
 */
@Service
public class ClassroomServiceImpl extends ServiceImpl<ClassroomMapper, Classroom> implements IClassroomService {
    private static IClassroomService instance;

    public static IClassroomService getInstance() {
        return instance;
    }

    @PostConstruct
    void init() {
        instance = this;
    }

    @Override
    public Result<IPage<ClassroomVO>> page(PageQuery pageQuery, String name, Integer category) {
        var wrapper = Wrappers.lambdaQuery(Classroom.class)
            .like(StrUtil.isNotBlank(name), Classroom::getName, name)
            .eq(category != null, Classroom::getCategory, category);

        return Result.ok(
            page(Page.of(pageQuery.getCurrent(), pageQuery.getSize()), wrapper)
                .convert(ClassroomVO::new)
        );
    }

}
